
package it.csttech.date;

import java.util.Date;

import javax.validation.ValidationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

/**
 * This class is meant to map a ValidationException into a response
 * 
 * @author marcello.felappi
 * @author marco.dalena
 */
@Provider
public class ValidationExceptionMapper
	implements ExceptionMapper<ValidationException> {

	/**
	 * This methods is used to translate a ValidationException into a BadRequest
	 * response with a json as entity representing a summary of the exception
	 * 
	 * @param exception
	 *            - ValidationException catched by the exception mapper
	 * @return the response representing the thrown exception
	 */
	@Override
	public Response toResponse(ValidationException exception) {

		String description =
			Messages.getString("ErrorCode.constraintViolation");
		Date serverLocalTime = new Date();
		Date occurence = new Date();
		String causeMessage = Messages.getString("ErrorCode.nullParameter");
		ErrorObject error =
			new ErrorObject(
				description, serverLocalTime, occurence, causeMessage);
		return Response.status(400).entity(error).build();
	}

}
